/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easyjetroster.controller;

import easyjetroster.model.Day;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author Jappo
 */
public class RosterParser {
    private Accumulator accumulator = new Accumulator();

    public void parse(File roster) throws FileNotFoundException {
        String content = getFileContent(roster);

        boolean startToRead = false;
        StringTokenizer tok = new StringTokenizer(content, ">");

        while(tok.hasMoreTokens()){
            String currentToken = tok.nextToken();
            if(currentToken.endsWith("</HEAD")){
                // sono alla fine del blocco head
                startToRead = true;
            }

            if(startToRead && currentToken.endsWith("\"S5\"")){
                // sto leggendo l'header di un giorno e il prossimo token contiene il giorno
                String day = getDay(tok.nextToken());
                if(day != null){
                    // consumo il nome del giorno
                    System.out.println(day);
                    
                    Day newDay = new Day();
                    newDay.setDate(getDate(day, "2011"));
                    accumulator.getDays().add(newDay);
                }
            }
        }
    }

    private String getFileContent(File roster) throws FileNotFoundException{
        StringBuilder text = new StringBuilder();
        String NL = System.getProperty("line.separator");
        Scanner scanner = new Scanner(new FileInputStream(roster));
        try {
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine()).append(NL);
            }
        } finally {
            scanner.close();
        }

        return text.toString();
    }

    private String getDay(String nextToken) {
        String possibleDay = nextToken.substring(0, 5);
        if(possibleDay.startsWith(" ") || isDayName(possibleDay)){
            return null; // non e' un giorno
        } else {
            return possibleDay;
        }
    }

    private boolean isDayName(String possibleDay) {
        try{
            // controllo che l'ultima cifra sia un numero
            Integer.parseInt(possibleDay.charAt(4) + "");
        } catch (Exception e){
            return true;
        }
        return false;
    }

    private GregorianCalendar getDate(String day, String year) {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.YEAR, Integer.parseInt(year));
        g.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day.substring(3, 5)));

        String month = day.substring(0, 3);

        if(month.equals("Jan")){
            g.set(Calendar.MONTH, Calendar.JANUARY);
        } else if(month.equals("Feb")){
            g.set(Calendar.MONTH, Calendar.FEBRUARY);
        } else if(month.equals("Mar")){
            g.set(Calendar.MONTH, Calendar.MARCH);
        } else if(month.equals("Apr")){
            g.set(Calendar.MONTH, Calendar.APRIL);
        } else if(month.equals("May")){
            g.set(Calendar.MONTH, Calendar.MAY);
        } else if(month.equals("Jun")){
            g.set(Calendar.MONTH, Calendar.JUNE);
        } else if(month.equals("Jul")){
            g.set(Calendar.MONTH, Calendar.JULY);
        } else if(month.equals("Aug")){
            g.set(Calendar.MONTH, Calendar.AUGUST);
        } else if(month.equals("Sep")){
            g.set(Calendar.MONTH, Calendar.SEPTEMBER);
        } else if(month.equals("Oct")){
            g.set(Calendar.MONTH, Calendar.OCTOBER);
        } else if(month.equals("Nov")){
            g.set(Calendar.MONTH, Calendar.NOVEMBER);
        } else if(month.equals("Dec")){
            g.set(Calendar.MONTH, Calendar.DECEMBER);
        }

        return g;
    }

}
